package cn.burnit.view;

import cn.burnit.pojo.Book;
import cn.burnit.service.BookService;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * 删除图书界面
 */
public class DeleteBookForm extends JFrame {

    private JLabel checkBookId,bookId,bookName,bookType,translator,publishTime,stock,price,publisher,author;
    private JTextField checkBookIdField,bookIdField,bookNameField,bookTypeField,translatorField,stockField,priceField,publisherField,authorField;
    private JButton btn_Del,btn_Cancel,btn_Check;
    final JXDatePicker datepick = new JXDatePicker();
    private BookService bookService = new BookService();

    public DeleteBookForm(){
        Container container = getContentPane();
        container.setLayout(null);

        btn_Del=new JButton("删除");
        btn_Del.setBounds(190,310,80,20);
        btn_Cancel=new JButton("关闭");
        btn_Cancel.setBounds(320,310,80,20);
        btn_Check=new JButton("查询");
        btn_Check.setBounds(360,10,80,20);
        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        btn_Check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkBook(e);
            }
        });
        btn_Del.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showConfirmDialog(null,"你确定要删除吗？");
                delBook(e);
            }
        });


        checkBookId=new JLabel("图书编号:");
        checkBookId.setFont(new Font("宋体", Font.PLAIN, 16));
        checkBookId.setBounds(120,10,100,20);

        bookId=new JLabel("图书编号:");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,50,100,20);
        bookType=new JLabel("图书类型:");
        bookType.setFont(new Font("宋体", Font.PLAIN, 16));
        bookType.setBounds(50,90,100,20);
        translator=new JLabel("译者:");
        translator.setFont(new Font("宋体", Font.PLAIN, 16));
        translator.setBounds(50,130,100,20);
        publishTime=new JLabel("出版时间:");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));
        publishTime.setBounds(50,170,100,20);
        stock=new JLabel("库存数量:");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(50,210,100,20);
        bookName=new JLabel("图书名称:");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(280,50,100,20);
        author=new JLabel("作者:");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(280,90,100,20);
        publisher=new JLabel("出版社:");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(280,130,100,20);
        price=new JLabel("定价:");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(280,170,100,20);

        checkBookIdField=new JTextField();
        checkBookIdField.setColumns(10);
        checkBookIdField.setBounds(200,10,130,20);

        bookIdField=new JTextField();
        bookIdField.setEditable(false);
        bookIdField.setBounds(130,50,130,20);
        bookTypeField=new JTextField();
        bookTypeField.setEditable(false);
        bookTypeField.setBounds(130,90,130,20);
        translatorField=new JTextField();
        translatorField.setEditable(false);
        translatorField.setBounds(130,130,130,20);
        Date date = new Date();
        // 设置 date日期
        datepick.setDate(date);
        datepick.setEditable(false);
        datepick.setBounds(130,170,130,20);
        stockField=new JTextField();
        stockField.setEditable(false);
        stockField.setBounds(130,210,130,20);
        bookNameField=new JTextField();
        bookNameField.setEditable(false);
        bookNameField.setBounds(360,50,130,20);
        authorField=new JTextField();
        authorField.setEditable(false);
        authorField.setBounds(360,90,130,20);
        publisherField=new JTextField();
        publisherField.setEditable(false);
        publisherField.setBounds(360,130,130,20);
        priceField=new JTextField();
        priceField.setEditable(false);
        priceField.setBounds(360,170,130,20);

        //添加到容器
        container.add(checkBookId);container.add(checkBookIdField);container.add(btn_Check);
        container.add(bookId);container.add(bookIdField);
        container.add(bookName);container.add(bookNameField);
        container.add(bookType);container.add(bookTypeField);
        container.add(author);container.add(authorField);
        container.add(translator);container.add(translatorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(btn_Del);container.add(btn_Cancel);


        setTitle("删除图书");
        setSize(600,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void delBook(ActionEvent e) {
        String checkId = this.checkBookIdField.getText();
        int i=bookService.delById(checkId);
        if (i>0){
            JOptionPane.showMessageDialog(null,"删除成功");
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }

    private void checkBook(ActionEvent e) {
        String id = this.checkBookIdField.getText();
        Book book=bookService.getBookById(id);
        if (book!=null){
            bookIdField.setText(book.getId());
            bookNameField.setText(book.getBookname());
            bookTypeField.setText(book.getBooktype());
            authorField.setText(book.getAuthor());
            translatorField.setText(book.getTranslator());
            publisherField.setText(book.getPublisher());
            datepick.setDate(book.getPublish_time());
            priceField.setText(String.valueOf(book.getPricce()));
            stockField.setText(String.valueOf(book.getStock()));
        }else {
            JOptionPane.showMessageDialog(null,"内容为空");
        }
    }

//    public static void main(String[] args) {
//        new DeleteBookForm();
//    }
}
